﻿using Leeder.Communi;
using Leeder.Interface.Communi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ControllerManageServer.PacketParsing
{
    /// <summary>
    /// DPS报文解析
    /// </summary>
    class DpsPacketParsing : IPacketParsing
    {

        /// <summary>
        /// 数据体长度的起始位置
        /// </summary>
        public const int PACKET_LENGTH_START_POSITION = 3;
        /// <summary>
        /// 数据包的基础长度
        /// </summary>
        public const int PACKET_BASE_LENGTH = 6;
        /// <summary>
        /// Socket对象
        /// </summary>
        public Socket SocketObj { get; set; }
        /// <summary>
        /// 待处理报文
        /// </summary>
        public byte[] PendingPacket { get; protected set; }
        /// <summary>
        /// 接收的报文
        /// </summary>
        public byte[] ReceivePacket { get; set; } = new byte[2 * 1024];

        /// <summary>
        /// 封装报文
        /// </summary>
        /// <param name="sendPacket">发送报文</param>
        /// <returns>返回封装报文</returns>
        public byte[] PackagePacket(byte[] sendPacket)
        {
            return sendPacket;
        }

        /// <summary>
        /// 解析报文
        /// </summary>
        /// <param name="receivePacket">接收报文</param>
        /// <returns>返回解析的报文列表</returns>
        public List<byte[]> ParsingPacket(byte[] receivePacket)
        {
            var packet = receivePacket.ToArray();
            List<byte[]> filterBytes = new List<byte[]>();
            if (PendingPacket == null || PendingPacket.Length == 0)
            {//不存在分包
                GetPacket(filterBytes, packet);
            }
            else
            {//存在分包
                byte[] mergePacket = new byte[PendingPacket.Length + packet.Length];
                Array.Copy(PendingPacket, mergePacket, PendingPacket.Length);
                Array.Copy(packet, 0, mergePacket, PendingPacket.Length, packet.Length);
                GetPacket(filterBytes, mergePacket);
            }
            return filterBytes;
        }

        /// <summary>
        /// 获取报文
        /// </summary>
        /// <param name="subpackageBytes">分包的报文列表</param>
        /// <param name="receivePacket">接收的报文</param>
        private void GetPacket(List<byte[]> subpackageBytes, byte[] receivePacket)
        {
            if (receivePacket == null || receivePacket.Length == 0) return;
            if (receivePacket.Length < PACKET_BASE_LENGTH)
            {
                PendingPacket = receivePacket;
                return;
            }
            UInt16 length = BitConverter.ToUInt16(receivePacket, PACKET_LENGTH_START_POSITION);
            if (receivePacket.Length < PACKET_BASE_LENGTH + length)
            {
                PendingPacket = receivePacket;
                return;
            }
            subpackageBytes.Add(receivePacket.Take(PACKET_BASE_LENGTH + length).ToArray());
            PendingPacket = new byte[0];
            GetPacket(subpackageBytes, receivePacket.Skip(PACKET_BASE_LENGTH + length).ToArray());
        }

    }
}
